﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HonBlogPub.Entity;
using HonBlogPub.Util;

namespace HonBlogPub
{
    public partial class frmMain : Form
    {
        private PostContext _postContext = new PostContext();

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnPublish_Click(object sender, EventArgs e)
        {
            BlogPost post = new BlogPost()
            {
                Title = txtTitle.Text,
                Context = _postContext,
                Content = txtCon.Text,
                Summary = txtSum.Text
            };
            post.AddTags(txtTags.Text.Trim());
            post.SetCategory(txtCat.Text.Trim());

            post.Publish();
        }

        private void SetFlagForContext(string flag)
        {
            if (0 == txtCon.SelectionLength)
                txtCon.Text += string.Format(" {0}{0} ", flag);
            else
                txtCon.SelectedText = string.Format(" {0}{1}{0} ", flag, txtCon.SelectedText);
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            SetFlagForContext("*");
        }

        private void btnItalic_Click(object sender, EventArgs e)
        {
            SetFlagForContext("_");
        }

        private void btnUnderline_Click(object sender, EventArgs e)
        {
            SetFlagForContext("+");
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            SetFlagForContext("-");
        }

        private void InsertHeadForLine(int headNum)
        {
            if (string.IsNullOrEmpty(txtCon.Text))
            {
                txtCon.Text = string.Format("h{0}. ", headNum);
                return;
            }

            string[] lines = txtCon.Lines;

            int row = TextBoxUtil.GetCurrentLine(txtCon);
            StringBuilder sb = new StringBuilder(txtCon.Text.Length + 5);

            for (int i = 0; i < row; i++)
            {
                sb.Append(lines[i]);
                sb.AppendLine();
            }

            sb.AppendLine();
            sb.AppendFormat("h{0}. {1}", headNum, lines[row]);
            sb.AppendLine();
            sb.AppendLine();

            for (int i = row + 1; i < lines.Length; i++)
            {
                sb.Append(lines[i]);
                sb.AppendLine();
            }

            txtCon.Text = sb.ToString();
        }

        private void btnH1_Click(object sender, EventArgs e)
        {
            InsertHeadForLine(1);
        }

        private void btnH2_Click(object sender, EventArgs e)
        {
            InsertHeadForLine(2);
        }

        private void btnH3_Click(object sender, EventArgs e)
        {
            InsertHeadForLine(3);
        }

        private void btnImage_Click(object sender, EventArgs e)
        {

        }

        private void btnPreview_Click(object sender, EventArgs e)
        {

        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }
    }
}
